package chapter14;

import java.util.*;

public abstract class GeometricObject implements Cloneable,Comparable{
	private String color = "white";
	private boolean filled;
	private Date dateCreate;
	
	protected GeometricObject(){
		dateCreate = new Date();
	}
	
	protected GeometricObject(String color, boolean filled){
		dateCreate = new Date();
		this.color = color;
		this.filled = filled;
	}
	
	public String getColor(){
		return color;
	}
	
	public void setColor(String color){
		this.color = color;
	}
	
	public boolean isFilled(){
		return filled;
	}
	
	public void setFilled(boolean filled){
		this.filled = filled;
	}
	
	public Date getDateCreate(){
		return dateCreate;
	}
	
	private void setDateCreate(long elapseTime){
		dateCreate = new Date(elapseTime);
	}
	
	public String toString(){
		return "created on " + dateCreate + "\ncolor: " + color + " and filled: " + filled;
	}
	
	public int compareTo(Object o){
		/*以面积为比较依据*/
		if (getArea() > ((GeometricObject)o).getArea())
			return 1;
		else if (getArea() < ((GeometricObject)o).getArea())
			return -1;
		else
			return 0;
	}

	
	public static GeometricObject max(GeometricObject o1, GeometricObject o2){
		if (o1.compareTo(o2) >= 0)
			return o1;
		else 
			return o2;
	}
	
	public abstract double getArea();
	
	public abstract double getPerimeter();
	
	public Object clone() throws CloneNotSupportedException{
		/*深克隆*/
		//return super.clone();
		long time = dateCreate.getTime();
		GeometricObject tmpObj = (GeometricObject) super.clone();
		tmpObj.setDateCreate(time);
		return tmpObj;
	}
}

